#!/bin/bash

DIRECTORY="$(dirname "$(dirname "$( cd "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )")")"

function error {
  echo -e "\\e[91m$1\\e[39m"
  exit 1
}

"${DIRECTORY}/pkg-install" "adb ffmpeg libsdl2-2.0-0 python3-pip python3-tk gcc git pkg-config meson ninja-build libavcodec-dev libavformat-dev libavutil-dev libsdl2-dev" "$(dirname "$0")" || exit 1

if [ -e ~/scrcpy ] && [ -e /usr/bin/adb];then
  #SCRCPY
  wget https://misapuntesde.com/rpi_share/scrcpy-1.13.tar.gz || error "Failed to download!"
  tar -xf scrcpy-1.13.tar.gz || error "Failed to extract!"
  rm scrcpy-1.13.tar.gz
  
  ln -s /usr/bin/adb $HOME/scrcpy/
  
  sudo mkdir -p /usr/local/share/scrcpy
  sudo cp -af $HOME/scrcpy/scrcpy-server /usr/local/share/scrcpy/scrcpy-server
  #END OF SCRCPY
fi

rm -rf ~/droidbuddy
git clone https://gitlab.com/gazlene/droidbuddy.git || error 'Could not clone AndroidBuddy repo!'

sudo pip3 install Pillow

mkdir -p ~/.local/share/applications
echo "[Desktop Entry]
Name=AndroidBuddy
Comment=GUI for Android device recovery and maintainence
Icon=$(dirname "$0")/icon-64.png
Exec=bash -c 'cd $HOME/droidbuddy ; python3 main.py'
Type=Application
Encoding=UTF-8
Terminal=false
Categories=Utility;" > ~/.local/share/applications/androidbuddy.desktop
